﻿using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Swirebev.PhotoDownload.Model;
using Swirebev.PhotoDownload.DAL;
using System.IO;
using Swirebev.PhotoDownload.Common;
using System.Reflection;

namespace Swirebev.PhotoDownload.Bll
{
	public class PhotoService
	{
		private const string _strSpliter = "\\";
		private const string _strPhototypename = "活动";

		/// <summary>
		/// 保存照片文件
		/// </summary>
		/// <param name="photo"></param>
		/// <param name="dataBuffer"></param>
		/// <returns></returns>
		public static bool SavePhoto(PhotoData photo, byte[] dataBuffer)
		{
			PhotoPath photoPath = SetPhotoPath(photo);
			string prjectstoreDirectory = photoPath.SaleCenterName + _strSpliter +
					photoPath.RouteId + _strSpliter +
					photoPath.PhotoTypeName + _strSpliter;
			try
			{
				if (photoPath.RouteId !=string.Empty && photoPath.PhotoTypeName != string.Empty && photoPath.SaleCenterName != string.Empty && photoPath.BusinessTypeCd =="420021") //活动检查图片
				{
					prjectstoreDirectory = Path.Combine(Settings.Instance.PhotoSavePath, prjectstoreDirectory);
					CreateDirectory(prjectstoreDirectory);
					using (FileStream fStream = new FileStream(prjectstoreDirectory + photo.PhotoName, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite))
					{
						fStream.Write(dataBuffer, 0, dataBuffer.Length);
					}
				}
				else if (photoPath.RouteId != string.Empty && photoPath.PhotoTypeName != string.Empty && photoPath.SaleCenterName != string.Empty && photoPath.BusinessTypeCd == "420020")//客户门招图片
				{
					prjectstoreDirectory = Path.Combine(Settings.Instance.CustomerphotoSavePath, prjectstoreDirectory);
					CreateDirectory(prjectstoreDirectory);
					using (FileStream fStream = new FileStream(prjectstoreDirectory + photo.PhotoName, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite))
					{
						fStream.Write(dataBuffer, 0, dataBuffer.Length);
					}
				}
				//保存到根目录下
				prjectstoreDirectory = Settings.Instance.photoSaveAllPath;
				CreateDirectory(prjectstoreDirectory);
				using (FileStream fStream = new FileStream(prjectstoreDirectory + photo.PhotoName, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite))
				{
					fStream.Write(dataBuffer, 0, dataBuffer.Length);
				}
			}
			catch(Exception ex)
			{
				Log.Error(MethodBase.GetCurrentMethod().DeclaringType, "保存照片文件时出错", ex);
				return false;
			}
			return true;
		}

		private static PhotoPath SetPhotoPath(PhotoData photo)
		{
			PhotoPath photoPath = new PhotoPath();
			photoPath.SaleCenterName = photo.RouteId == null ? string.Empty : GetSalesCenterByRoute(photo.RouteId);
			photoPath.PhotoVisitType = "3";
			photoPath.PhotoTypeName = _strPhototypename;
			photoPath.StoreName = photo.StoreId == null ? string.Empty : photo.StoreId;
			photoPath.RouteId = photo.RouteId == null ? string.Empty : photo.RouteId;
			photoPath.PhotoVisitTypeStr = String.Empty;
			photoPath.BusinessTypeCd = photo.BusinessTypeCd == null ? string.Empty : photo.BusinessTypeCd;
			return photoPath;
		}

		/// <summary>
		/// 取营业所名称
		/// </summary>
		/// <param name="routeId"></param>
		/// <returns></returns>
		public static string GetSalesCenterByRoute(string routeId)
		{
			return LocationDataAccess.GetSalesCenterByRoute(routeId);
		}

		/// <summary>
		/// 创建文件夹
		/// </summary>
		/// <param name="directoryName"></param>
		private static void CreateDirectory(string directoryName)
		{
			if (!Directory.Exists(directoryName))
			{
				Directory.CreateDirectory(directoryName);
			}
		}
	}
}
